<template>
  <div class="new-page assessmentRecord">
    <!-- 查询 -->
    <div class="search margin-bot">
      <a-input-search placeholder="请输入考核名称查询" style="width: 200px" @search="onSearch" />
    </div>
    <div class="margin-bot">
      <a-button type="primary" icon="plus" @click="showModal">新增</a-button>
    </div>
    <!-- 表格 -->
    <div class="margin-bot">
      <a-table :columns="columns" :data-source="data">
        <span slot="action" slot-scope="text, record">
          <a style="margin-right:30px;color:#1890FF" @click="onEdit(text, record)">编辑</a>
          <a style="color:#1890FF" @click="onDelete(text, record)">删除</a>
        </span>
         <span slot="remarks" slot-scope="text">
          <a-tooltip placement="top">
            <template slot="title">
              <span>{{text}}</span>
            </template>
            <div class="remarks">{{text}}</div>
          </a-tooltip>
        </span>
        <div
          slot="expandedRowRender"
          slot-scope="record"
          v-if="record.description.data.length!=0"
          style="margin: 0;"
        >
          <div class="flexStart">
            <div class="flexCenter">
              <div>考核资料：</div>
              <div>
                <div v-for="(item,index) in record.description.data" :key="index">
                  <span class="checkInfo">{{item}}</span>
                </div>
              </div>
            </div>
            <!-- <div>创建人：{{record.description.people}}</div> -->
            <!-- <div>创建人：{{record.description.updata}}</div> -->
          </div>
        </div>
      </a-table>
    </div>
    <!-- 分页 -->
    <div class="pagination flexBetween">
      <div>共{{total}} 条记录 第 {{current}} / {{Math.ceil((total/pageSize))}} 页</div>
      <div>
        <a-pagination
          v-model="current"
          :page-size-options="pageSizeOptions"
          :total="total"
          show-size-changer
          :page-size="pageSize"
          @showSizeChange="onShowSizeChange"
        >
          <template slot="buildOptionText" slot-scope="props">
            <span v-if="props.value !== '50'">{{ props.value }}条/页</span>
            <span v-if="props.value === '50'">全部</span>
          </template>
        </a-pagination>
      </div>
    </div>
    <!-- 对话框 -->
    <a-modal v-model="visible" :title="modalTitle" @ok="handleOk" :maskClosable="false">
      <a-form-model
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        v-if="visible"
      >
        <a-form-model-item ref="name" label="考核名称" prop="name">
          <a-input
            v-model="form.name"
            @blur="
          () => {
            $refs.name.onFieldBlur();
          }
        "
          />
        </a-form-model-item>

        <a-form-model-item ref="obj" label="考核对象" prop="obj">
          <a-input
            v-model="form.obj"
            @blur="
          () => {
            $refs.obj.onFieldBlur();
          }
        "
          />
        </a-form-model-item>

        <a-form-model-item ref="result" label="考核结果" prop="result">
          <a-input
            v-model="form.result"
            @blur="
          () => {
            $refs.obj.onFieldBlur();
          }
        "
          />
        </a-form-model-item>

        <a-form-model-item label="考核日期" required prop="time">
          <a-date-picker
            v-model="form.time"
            type="date"
            placeholder="Pick a date"
            style="width: 100%;"
          />
        </a-form-model-item>

        <a-form-model-item label="备注说明">
          <a-input v-model="form.desc" type="textarea" />
        </a-form-model-item>

        <a-form-model-item label="考核资料" prop="formUpload">
          <a-upload
            name="file"
            :multiple="true"
            action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
            :headers="headers"
            @change="handleChange"
          >
            <a-button>
              <a-icon type="upload" />上传文件
            </a-button>
            <div style="color:#ccc">请上传pdf文件</div>
          </a-upload>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import moment from "moment";
export default {
  data() {
    let formUpload = (rule, value, callback) => {
      if (this.pdfData === "") {
        callback(new Error("请上传文件"));
      } else {
        this.$refs.ruleForm.validateField("formUpload");
        callback();
      }
    };
    return {
      columns: [
        { title: "考核名称", dataIndex: "name", key: "name" },
        { title: "考核对象", dataIndex: "age", key: "age" },
        { title: "考核结果", dataIndex: "result", key: "result" },
        { title: "考核日期", dataIndex: "address", key: "address" },
        { title: "备注", dataIndex: "remarks", key: "remarks" ,
          scopedSlots: { customRender: "remarks" }},

        {
          title: "操作",
          key: "action",
          scopedSlots: { customRender: "action" },
          align: "center"
        }
      ],
      data: [
        {
          key: 1,
          name: "2021年保障农民工工资支付自查考核",
          age: "指挥部",
          result: "87",
          address: "2021-2-22",
          remarks: "第二次进行",

          description: {
            data: [
              "2021年度第二次进行农民工考核.pdf",
              "2021年度第二次进行农民工考核.pdf"
            ]
            // people: "张三  2020-02-21 09:22:32",
            // updata: "张三  2020-02-21 09:22:32"
          }
        },
        {
          key: 2,
          name: "2021年上半保障农民工工资支付自查",
          age: "项目部",
          result: "优秀",
          address: "2021-2-22",
          description:{data:[
               "2021年度第二次进行农民工考核.pdf",
              "2021年度第二次进行农民工考核.pdf"
          ]}
         
        }
      ],
      pageSizeOptions: ["10", "20", "30", "40", "50"],
      current: 1,
      pageSize: 10,
      total: 50,
      modalTitle: "新增考核记录",
      visible: false,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      form: {
        name: "",
        obj: "",
        result: "",
        desc:'',
        time: new Date().toLocaleDateString()
      },
      defaultTime: "",
      rules: {
        name: [
          {
            required: true,
            message: "请输入考核名称",
            trigger: "change"
          }
        ],
        obj: [
          {
            required: true,
            message: "请输入考核对象",
            trigger: "change"
          }
        ],
        result: [
          {
            required: true,
            message: "请输入考核结果",
            trigger: "change"
          }
        ],
        // desc: [
        //   {
        //     required: true,
        //     message: "请输入考核结果",
        //     trigger: "change"
        //   }
        // ],
        time: [
          { required: true, message: "请选择考核时间", trigger: "change" }
        ],
        formUpload: [
          { required: true, validator: formUpload, trigger: "change" }
        ]
      },
      headers: {
        authorization: "authorization-text"
      },
      pdfData: "",
    };
  },
  mounted() {
    this.defaultTime = new Date().toLocaleDateString();
      
    // console.log(this.defaultTime);
  },
  methods: {
    moment,
    onEdit() {
      this.modalTitle = "编辑考核记录";
      this.visible = true;
    },
    onDelete() {},
    onSearch(value) {
      console.log(value);
    },
    onShowSizeChange(current, pageSize) {
      this.pageSize = pageSize;
    },
    showModal() {
      this.visible = true;
    },
    handleOk(e) {
      this.pdfData = "";
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // alert('submit!');
          this.visible = false;
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    handleChange(info) {
      if (info.file.status !== "uploading") {
        this.pdfData = info.file;
        // console.log(info.file, info.fileList);
      }
      if (info.file.status === "done") {
        // this.pdfData=info.file
        // console.log(info.file);

        this.$message.success(`${info.file.name} 上传成功`);
      } else if (info.file.status === "error") {
        this.$message.error(`${info.file.name} 上传失败`);
      }
    },
  
       

    // beforeUpload(file) {
    //   this.fileList = [...this.fileList, file];
    //   console.log(this.fileList);

    //   return false;
    // },
  }
};
</script>

<style scoped>
.search {
  padding-top: 30px;
  display: flex;
  justify-content: flex-end;
}
.assessmentRecord {
  background: #fff;
}
.margin-bot {
  margin-bottom: 30px;
}
::v-deep .ant-pagination-options-size-changer.ant-select {
  margin-right: 0;
}
.checkInfo:hover {
  color: #1890ff;
  cursor: pointer;
}
.tdShow{
    display: none;
}
.remarks{
  white-space: nowrap;overflow: hidden;text-overflow: ellipsis;
  cursor: pointer;
  width: 85px;
}
</style>